#include "titlebar.h"
#include "frameless/framelesswidget.h"

#include <QPainter>
#include <QStyle>
#include <QStyleOption>
#include <QApplication>
#include <QDesktopWidget>
#include <QMouseEvent>
#include <QDebug>
#include <QMdiSubWindow>
#include <QMdiArea>
#include <QMenu>
#include <QAction>
/*---------------------------------*/

TitleButton::TitleButton(QWidget *parent) : QPushButton(parent)
{
    setStyleSheet("QPushButton{background:transparent;}");
}
TitleButton::~TitleButton()
{
}

void TitleButton::loadPixmap(const QString& picName)
{
    pixmapType = FOREGROUND;
    pixmap.load(picName);
    width = pixmap.width() / 4;//4个状态
    height = pixmap.height();
    setFixedSize(width, height);
}

void TitleButton::setPixmap(const QPixmap& _pixmap)
{
    pixmapType = FOREGROUND;
    pixmap = _pixmap;
    width = pixmap.width() / 4;//4个状态
    height = pixmap.height();
    setFixedSize(width, height);
}

void TitleButton::loadBackground(const QString& picName)
{
    loadPixmap(picName);
    pixmapType = BACKGROUND;
}

void TitleButton::setBackground(const QPixmap& _pixmap)
{
    setPixmap(_pixmap);
    pixmapType = BACKGROUND;
}


void TitleButton::enterEvent(QEvent *)
{
    buttonStates = HOVER;
    update();
}
void TitleButton::leaveEvent(QEvent *)
{
    buttonStates = NORMAL;
    update();
}
void TitleButton::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        pressed = true;
        buttonStates = PRESSED;
        update();
    }
    QPushButton::mousePressEvent(event);
}
void TitleButton::mouseReleaseEvent(QMouseEvent *event)
{
    if(pressed) {
        pressed = false;
        if(this->rect().contains(event->pos()))
            buttonStates = HOVER;
        else
            buttonStates = NORMAL;
        update();
    }
    QPushButton::mouseReleaseEvent(event);
}
void TitleButton::paintEvent(QPaintEvent * e)
{
    if(pixmapType == BACKGROUND)
        paintPixmap();
    QPushButton::paintEvent(e);
    if(pixmapType == FOREGROUND)
        paintPixmap();
}
void TitleButton::paintPixmap()
{
    QPainter painter(this);

    //根据状态显示图片
    ButtonStatus states = buttonStates;
    if(!isEnabled())
        states = DISABLED;
    painter.drawPixmap(rect(), pixmap.copy(width * states, 0, width, height));
}



/*---------------------------------*/

MainWindowTitleBar::MainWindowTitleBar(TitleBarElementTypes types, QWidget *parent): QWidget(parent)
{
    // 标题栏高度
    setFixedHeight(32);
    //布局
    titleLayout = new QHBoxLayout(this);
    titleLayout->setSpacing(1);
    titleLayout->setMargin(1);

    // 默认的icon图标
    if(types.testFlag(ICON))
    {
        icon = new QLabel(this);
        icon->setObjectName("titleBarIcon");
        icon->setPixmap(QPixmap(":/pic/icon.png"));
        icon->setFixedSize(16, 16);
        icon->setScaledContents(true);
        titleLayout->addWidget(icon, 0, Qt::AlignVCenter);
        titleLayout->addSpacing(5);
        window()->setWindowIcon(QPixmap(":/pic/icon.png"));
    }
    if(types.testFlag(TITLE))
    {
        title = new QLabel("应用程序");
        title->setObjectName("titleBarTitle");
        titleLayout->addWidget(title, 0, Qt::AlignVCenter);
        titleLayout->addStretch();
    }
    if(types.testFlag(SKIN))
    {
        skinButton = new TitleButton(this);
        skinButton->setObjectName("titleBarSkin");
        skinButton->setPixmap(QPixmap(":/pic/skin.png"));
        titleLayout->addWidget(skinButton, 0, Qt::AlignTop);


        QMenu *skinMenu = new QMenu;
        QAction *blueSkinAction = new QAction(skinMenu);
        blueSkinAction->setText("蓝色主题");
        connect(blueSkinAction, &QAction::triggered, parent,
                [=]()
                {
                    emit changeSkin(QString(":/blue.qss"));
                });
        skinMenu->addAction(blueSkinAction);

        QAction *darkSkinAction = new QAction(skinMenu);
        darkSkinAction->setText("黑色主题");
        connect(darkSkinAction, &QAction::triggered, this,
                [=]()
                {
                    emit changeSkin(QString(":/dark.qss"));

                });
        skinMenu->addAction(darkSkinAction);

        QAction *orangeSkinAction = new QAction(skinMenu);
        orangeSkinAction->setText("橙色主题");
        connect(orangeSkinAction, &QAction::triggered, this,
                [=]()
                {
                    emit changeSkin(QString(":/orange.qss"));

                });
        skinMenu->addAction(orangeSkinAction);

        skinButton->setMenu(skinMenu);

    }
    if(types.testFlag(MENU))
    {
        menuButton = new TitleButton(this);
        menuButton->setObjectName("titleBarMenu");
        menuButton->setPixmap(QPixmap(":/pic/menu.png"));
        titleLayout->addWidget(menuButton, 0, Qt::AlignTop);
    }
    if(types.testFlag(MIN))
    {
        minButton = new TitleButton(this);
        minButton->setObjectName("titleBarMin");
        minButton->setPixmap(QPixmap(":/pic/min.png"));
        titleLayout->addWidget(minButton, 0, Qt::AlignTop);

        connect(minButton, &TitleButton::clicked, this,
                [=]()
                {
                    window()->showMinimized();
                });
    }
    if(types.testFlag(MAX))
    {
        maxButton = new TitleButton(this);
        maxButton->setObjectName("titleBarMax");
        maxPixmap = QPixmap(":/pic/max.png");
        maxButton->setPixmap(maxPixmap);
        titleLayout->addWidget(maxButton, 0, Qt::AlignTop);

        connect(maxButton, &TitleButton::clicked, this, &showMaxState);

    }
    if(types.testFlag(RESTORE))
    {
        restorePixmap = QPixmap(":/pic/restore.png");
    }
    if(types.testFlag(CLOSE))
    {
        closeButton = new TitleButton(this);
        closeButton->setObjectName("titleBarClose");
        closeButton->setPixmap(QPixmap(":/pic/close.png"));
        titleLayout->addWidget(closeButton, 0, Qt::AlignTop);

        connect(closeButton, &TitleButton::clicked, this,
                [=]()
                {
                    window()->close();
                });
    }



    setLayout(titleLayout);
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
}
MainWindowTitleBar::~MainWindowTitleBar()
{
}
/************************************************************************
* 函数功能: 给标题栏设定默认外的图标或文字
* 输入参数1: 标题栏元素类型
* 输入参数2: 对应的内容，元素为图标时此为图标绝对路径，元素为title时此为title字符串，
* 返回值: 无
* 日期: 2017年11月15日
*  By sun chao
***********************************************************************/
void MainWindowTitleBar::setElement(TitleBarElementType type, const QString content)
{

    switch (type) {
    case ICON:
    {
        if(icon)
            icon->setPixmap(QPixmap(content));
        break;
    }
    case TITLE:
    {
        if(title)
            title->setText(content);
        break;
    }
    case MENU:
    {
        if(menuButton)
            menuButton->setPixmap(QPixmap(content));
        break;
    }
    case SKIN:
    {
        if(skinButton)
            skinButton->setPixmap(QPixmap(content));
        break;
    }
    case MIN:
    {
        if(minButton)
            minButton->setPixmap(QPixmap(content));
        break;
    }
    case MAX:
    {
        if(maxButton)
            maxButton->setPixmap(QPixmap(content));
        break;
    }
    case RESTORE:
    {
        restorePixmap = QPixmap(content);

        break;
    }
    case CLOSE:
    {
        if(closeButton)
            closeButton->setPixmap(QPixmap(content));
        break;
    }
    default:
        break;
    }

}

void MainWindowTitleBar::showMaxState()
{
    if(maxed) {
        maxed = !maxed;
        window()->setGeometry(normalRect);
        maxButton->setPixmap(maxPixmap);
    }
    else {
        maxed = !maxed;
        normalRect = window()->geometry();
        window()->setGeometry(QApplication::desktop()->availableGeometry());
        maxButton->setPixmap(restorePixmap);

    }
}


void MainWindowTitleBar::mousePressEvent(QMouseEvent *event)
{
    QRect rt = rect();
    if(rt.contains(event->pos())) {
        if(event->button() == Qt::LeftButton) {
            pressed = true;
            clickPos = mapToParent(event->pos());
            event->accept();
            return;
        }
    }
    event->ignore();
}

void MainWindowTitleBar::mouseReleaseEvent(QMouseEvent *event)
{
    pressed = false;
    event->ignore();
}

void MainWindowTitleBar::mouseMoveEvent(QMouseEvent *event)
{
    if(pressed && !maxed) {
        window()->move(event->globalPos() - clickPos);
        event->accept();
    }
    else {
        event->ignore();
    }
}

void MainWindowTitleBar::mouseDoubleClickEvent(QMouseEvent *event)
{
    Q_UNUSED(event);
    if(maxButton)
        showMaxState();
}

void MainWindowTitleBar::paintEvent(QPaintEvent* e)
{
    Q_UNUSED(e)
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
/*---------------------------------*/
widgetTitleBar::widgetTitleBar(QWidget *parent) : QWidget(parent)
{
    if(parent) {
        parentSubWindow = qobject_cast<QWidget*>(parent->parent());
    }
    // 标题栏高度
    setFixedHeight(24);
    // 布局
    titleLayout = new QHBoxLayout(this);
    titleLayout->setSpacing(1);
    titleLayout->setMargin(1);


    // 默认的标题
    title = new QLabel("方案配置");
    title->setObjectName("titleBarTitle");
    titleLayout->addWidget(title, 0, Qt::AlignVCenter);
    titleLayout->addStretch();

    // 关闭按钮
    closeButton = new TitleButton(this);
    closeButton->setPixmap(QPixmap(":/pic/close.png"));
    titleLayout->addWidget(closeButton, 0, Qt::AlignTop);

    connect(closeButton, &TitleButton::clicked, this,
            [=]()
            {
                emit closeParent();
            });

    setLayout(titleLayout);
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);


}
widgetTitleBar::~widgetTitleBar()
{
}

void widgetTitleBar::setTitle(const QString &_title)
{
    if(!_title.isEmpty())
        title->setText(_title);
}


void widgetTitleBar::paintEvent(QPaintEvent* e)
{
    Q_UNUSED(e)
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}



